package com.example.petcare.utils;


import lombok.extern.slf4j.Slf4j;

/**
 * ThreadLocal 工具类
 */
@SuppressWarnings("all")
@Slf4j
public class ThreadLocalUtil {
    // 提供ThreadLocal对象
    private static final ThreadLocal<Object> THREAD_LOCAL = new ThreadLocal<>();
    private static final ThreadLocal<String> ROLE_LOCAL = new ThreadLocal<>();

    // 根据键获取值
    public static <T> T get() {
        return (T) THREAD_LOCAL.get();
    }

    // 存储业务数据
    public static void set(Object value) {
        THREAD_LOCAL.set(value);
        log.info("本地工具类 set value: {}", value);
    }

    // 存储用户角色
    public static void setRole(String role) {
        ROLE_LOCAL.set(role);
    }

    // 获取用户角色
    public static String getRole() {
        return ROLE_LOCAL.get();
    }

    // 清除ThreadLocal，防止内存泄漏
    public static void remove() {
        THREAD_LOCAL.remove();
        ROLE_LOCAL.remove(); // 同时清除角色信息
    }
}
